﻿$(document).ready(function () {
    $("#tabs").tabs();
    $("a.change_lang").click(function () {
        var self = this;
        var lang = $(self).attr('rel');

        $.ajax({
            url: "/Settings/language/update",
            type: 'POST',
            data: { lang_name: lang },
            beforeSend: function () {
                $(self).children("img:nth-child(1)").attr('src', 'images/flag_loader.gif');
                $(self).children("img:nth-child(1)").attr('height', '15');
            },
            success: function (response) {
                //alert(response);											
                var json_arr = eval("(" + response + ")");

                if (json_arr.status == 'ok') {
                    document.location.href = 'HOME';
                }
            }
        });
    });

    $("a.jQueryBookmark").click(function (e) {

        e.preventDefault(); // this will prevent the anchor tag from going the user off to the link

        var bookmarkUrl = this.href;

        var bookmarkTitle = this.title;



        if (window.sidebar) { // For Mozilla Firefox Bookmark

            window.sidebar.addPanel(bookmarkTitle, bookmarkUrl, "");

        } else if (window.external || document.all) { // For IE Favorite

            window.external.AddFavorite(bookmarkUrl, bookmarkTitle);

        } else if (window.opera) { // For Opera Browsers

            $("a.jQueryBookmark").attr("href", bookmarkUrl);

            $("a.jQueryBookmark").attr("title", bookmarkTitle);

            $("a.jQueryBookmark").attr("rel", "sidebar");

        } else { // for other browsers which does not support

            alert('Your browser does not support this bookmark action');

            return false;

        }

    });

    $('div.info-class[title]').qtip({
        position: { my: 'bottom left', at: 'top center' },
        show: { target: false, event: 'mouseover', effect: function (offset) { $(this).show(300); } },
        hide: { event: 'mouseout', effect: function (offset) { $(this).hide(300); } },
        style: { classes: 'ui-tooltip-frontend' }
    });


    autoSugg();
    $('#country_id_H').change(function () {
        if ($('#country_id_H option:selected').val() == 'any' || $('#country_id_H option:selected').val() == '') {
            $('#area_id_H').html('<option value="any">Select Area....</option>');
            $('#state_id_H').html('<option value="any">Select State....</option>');
        }
        else {
            $.ajax({
                url: "/Tours/frontend/states",
                type: 'POST',
                data: { id: $('#country_id_H option:selected').val() },
                beforeSend: function () {
                    $('#state_id_H').html('<option value="any">Loading Data.....</option>');
                    $('#area_id_H').html('<option value="any">Select Area....</option>');
                    $('#state_id_err').html('<img src="images/ajax_loader7.gif" border="0" width="15" height="15" />');
                },
                success: function (response) {
                    //alert(response);
                    var json_arr = eval("(" + response + ")");
                    var option = '<option value="any">Select State....</option>';
                    if (json_arr.status == 'ok') {
                        for (var i = 0; i < json_arr.states.length; i++) {
                            option += '<option value="' + json_arr.states[i].state_id + '" >' + stripslashes(json_arr.states[i].state_name) + '</option>';
                        }
                        $('#state_id_H').html(option);
                    }
                    else {
                        $('#state_id_H').html('<option value="any">' + json_arr.msg + '</option>');
                    }
                    $('#state_id_err').html('');
                }
            });
        }
    });

    $('#state_id_H').change(function () {

        if ($('#state_id_H option:selected').val() == 'any' || $('#state_id_H option:selected').val() == '') {
            $('#area_id_H').html('<option value="any">Select Area....</option>');
        }
        else {
            $.ajax({
                url: "/Tours/frontend/areas",
                type: 'POST',
                data: { id: $('#state_id_H option:selected').val() },
                beforeSend: function () {
                    $('#area_id_H').html('<option value="any">Loading Data.....</option>');
                    $('#area_id_err').html('<img src="images/ajax_loader7.gif" border="0" width="15" height="15" />');
                },
                success: function (response) {
                    //alert(response);
                    var json_arr = eval("(" + response + ")");
                    var option = '<option value="any">Select Area....</option>';
                    if (json_arr.status == 'ok') {
                        for (var i = 0; i < json_arr.cities.length; i++) {
                            option += '<option value="' + json_arr.cities[i].city_id + '" >' + stripslashes(json_arr.cities[i].city) + '</option>';
                        }
                        $('#area_id_H').html(option);
                    }
                    else {
                        $('#area_id_H').html('<option value="any">' + json_arr.msg + '</option>');
                    }
                    $('#area_id_err').html('');
                }
            });
        }
    });

    $('input.search_btn').click(function () {
        var self = this;
        $('#formSearch').submit();
    });
});

function openMsgDialog(msg) {
    $('#dialog_container').html(msg).dialog({
        autoOpen: true,
        modal: false,
        show: 'explode',
        resizable: 'false',
        buttons: { "Ok": function () { $(this).dialog('option', 'hide', 'explode').dialog("close"); } },
        open: function (event, ui) {
            setTimeout(function () {
                if ($('#' + event.target.id).dialog('isOpen')) {
                    //$('#' + event.target.id).dialog('option', 'hide', 'explode').dialog('close');										
                }
            }, 5000);
        }
    });
}

function autoSugg() {
    $("#post_code").autocomplete({
        source: function (request, response) {
            $.ajax({
                url: "/Tours/frontend/searchname",
                type: 'POST',
                dataType: "json",
                data: {
                    post_code: request.term
                },
                success: function (data) {
                    if (data.status == 'ok') {
                        response($.map(data.search_data, function (item) {
                            var traveler_photos = item.traveler_photos;
                            var tours_image = item.tours_image;
                            var img_arr = (traveler_photos) ? traveler_photos.split(',') : tours_image.split(',');
                            var room = item.tours_location.split('<br />');
                            return {
                                label: item.tours_name,
                                value: item.post_code,
                                pic: '<img src="images/frontImages/tours/tours_image/' + img_arr[0] + '" width="25" height="20" />',
                                room: item.tours_room,
                                owner: item.owner_name,
                                location: room[1]
                            }
                        }));
                    }
                    else {
                        response($.map(data, function (item) {
                            return
                        }));
                    }
                }
            });
        },
        minLength: 1,
        open: function () {
            $(this).removeClass("ui-corner-all").addClass("ui-corner-top");
        },
        close: function () {
            $(this).removeClass("ui-corner-top").addClass("ui-corner-all");
        }
    })
		.data("autocomplete")._renderItem = function (ul, item) {
		    return $("<li></li>")
				.data("item.autocomplete", item)
				.append("<a style='font-size:12px;color:#2A3F00;width:300px;font-weight:normal;'>" + item.pic + " " + item.label + item.owner + " <strong>Postcode :</strong> " + item.value + item.room + " " + item.location + "</a>")
				.appendTo(ul);
		};
}
function stripslashes(str) {
    str = str.replace(/\\'/g, '\'');
    str = str.replace(/\\"/g, '"');
    str = str.replace(/\\0/g, '\0');
    str = str.replace(/\\\\/g, '\\');
    return str;
}